﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Gammar.Digger.SystemFramework
{
    /// <summary>
    /// 防止DDOS攻击类
    /// </summary>
    public class ActionValidator
    {
        private const int DURATION = 10; // 10 min period

        public enum ActionTypeEnum
        {
            FirstVisit = 100, // The most expensive one, choose the value wisely. 
            ReVisit = 1000,  // Welcome to revisit as many times as user likes
            Postback = 5000,    // Not must of a problem for us
            AddNewWidget = 100,
            AddNewPage = 100,
        }

        //public static bool IsValid(ActionTypeEnum actionType)
        //{
        //    HttpContext context = HttpContext.Current;
        //    if (context.Request.Browser.Crawler) return false;

        //    string key = actionType.ToString() + context.Request.UserHostAddress;
        //    var hit= (HitInfo)(context.Cache[key] ?? new HitInfo());

        //    if (hit.Hits > (int)actionType) return false;
        //    else hit.Hits++;

        //    if (hit.Hits == 1)
        //        context.Cache.Add(key, hit, null, DateTime.Now.AddMinutes(DURATION),
        //           System.Web.Caching.Cache.NoSlidingExpiration,
        //           System.Web.Caching.CacheItemPriority.Normal, null);
        //    return true;
        //}

    }

    #region demo
//    The usage is very simple, on the default.aspx: 

//protected override void OnInit(EventArgs e)
//{
//   base.OnInit(e);

//   // Check if revisit is valid or not
//   if( !base.IsPostBack ) 
//   {
//      // Block cookie less visit attempts
//      if( Profile.IsFirstVisit )
//      {
//         if( !ActionValidator.IsValid(ActionValidator.ActionTypeEnum.FirstVisit))  
//            Response.End();
//      }
//      else
//      {
//         if( !ActionValidator.IsValid(ActionValidator.ActionTypeEnum.ReVisit) )  
//            Response.End();
//      }
//   }
//   else
//   {
//      // Limit number of postbacks
//      if( !ActionValidator.IsValid(ActionValidator.ActionTypeEnum.Postback) ) 
//            Response.End();
//   }
//}

    #endregion
}
